package cloudInformation.config;

import cn.hutool.core.util.ObjUtil;
import feign.RequestInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @program: Smart School
 * @ClassName OpenFeignConfig
 * @description: OpenFeign 配置类 用于 传递 我 的 token
 * @author: 陈智博
 * @create: 2025-04-15 09:09
 * @Version 1.0
 **/
@Configuration
public class OpenFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {

        return requestTemplate -> {

            String token;
            // 请求方
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (ObjUtil.isEmpty(requestAttributes)) {
                return;
            } else {
                // 获取请求方token
                HttpServletRequest request = requestAttributes.getRequest();
                token = request.getHeader("Authorization");
            }
            // 被请求方设置token，实现token中转
            requestTemplate.header("Authorization", token);
        };
    }

}
